﻿using System.Windows;
using System.Windows.Media;
using yzrilyzr.util;

namespace FAQ_UI
{
	public abstract class Drawable:IMutatable<Drawable>
	{
		public Rect bounds = new Rect();
		private Rect newBounds = new Rect();
		private InvalidateListener mInvalidateListener;
		protected Logger logger = new Logger("Drawable");
		public bool invalidate = false;

		public void setInvalidateListener(InvalidateListener l)
		{
			this.mInvalidateListener = l;
		}
		public MyDrawableView View { get;internal set; }
		public abstract void draw(DrawingContext canvas);

		public virtual Drawable mutate()
		{
			return this;
		}

		public void invalidateSelf()
		{
			if (mInvalidateListener == null) logger.w("No InvalidateListener Bind");
			else if (!invalidate)
			{
				invalidate = true;
				mInvalidateListener.drawableInvalidate();
			}
		}

		public void setBounds(Rect bounds)
		{
			setBounds(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
		}

		public void setBounds(double l, double t, double r, double b)
		{
			newBounds.X = l;
			newBounds.Y = t;
			newBounds.Width = r - l;
			newBounds.Height = b - t;
			if (newBounds != bounds) onBoundsChange(bounds, newBounds);
			bounds.X = newBounds.X;
			bounds.Y = newBounds.Y;
			bounds.Width = newBounds.Width;
			bounds.Height = newBounds.Height;
		}

		public virtual double getIntrinsicWidth()
		{
			return 0;
		}

		public virtual double getIntrinsicHeight()
		{
			return 0;
		}

		protected virtual void onBoundsChange(Rect oldBounds, Rect newBounds)
		{
		}

		public interface InvalidateListener
		{
			void drawableInvalidate();
		}

		public virtual FrameworkElement getChild()
		{
			return null;
		}
	}
}